<?php

namespace Summer\Hjd\Http\Controllers;

use Illuminate\Routing\Controller;
use RLuders\JWTAuth\Classes\JWTAuth;
use Summer\Hjd\Classes\CommonApi;
use Summer\Hjd\Http\Requests\WdRecordCreateRequest;
use Summer\Hjd\Models\WalletModel;
use Summer\Hjd\Models\WdRuleModel;
use Summer\Hjd\Models\WdRecordModel;
use Summer\Hub\Http\Controllers\Traits\ApiReturn;
use Input;
use Config;
use Db;
use Exception;
use System\Models\File as FileModel;


class WdRecordController extends Controller
{
    use ApiReturn;

    /**
     * 提现申请
     * @param JWTAuth $auth
     */
    public function create(JWTAuth $auth,WdRecordCreateRequest $request){
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $request->validate();
        $currency_slug=Input::get('currency_slug');
        $money=Input::get('money');
        $pay_type=Input::get('pay_type');
        $remark_user=Input::get('remark_user');
        $pay_type_config=Config::get('summer.hjd::wd_pay_type');
        if(!array_key_exists($pay_type,$pay_type_config)){
            return $this->apiError('请选择正确的收款方式');
        }
        //提现规则
        $rule=WdRuleModel::with(['currency'=>function($q){
            $q->select('slug','title','price');
        }])->whereHas('currency',function ($q){
            $q->where('status',1);
        })
        ->where('currency_slug',$currency_slug)
        ->first();
        if(!$rule){
            return $this->apiError('提现规则获取失败');
        }
        if($rule->status!=1){
            return $this->apiError('该币种提现暂时关闭');
        }
        //提现日期限制
        $wd_days=$rule->wd_days;
        if($wd_days){//有值才做限制
            //j	月份中的第几天，没有前导零	1 到 31
            $day=date('j');
            if(!in_array($day,$wd_days)){
                return $this->apiError($day.'号不能提现');
            }
        }
        //星期限制
        $wd_weeks=$rule->wd_weeks;
        if($wd_weeks){//有值才做限制
            //N	ISO-8601 格式数字表示的星期中的第几天（PHP 5.1.0 新加）	1（表示星期一）到 7（表示星期天）
            $week=date('N');
            if(!in_array($week,$wd_weeks)){
                return $this->apiError(Config::get('summer.hjd::wd_status.'.$week).'不能提现');
            }
        }
        //时间限制
        $Hi=date('H:i:s');
        $wd_star=$rule->wd_star;
        if($wd_star){
            if($Hi<$wd_star){
                return $this->apiError($wd_star.'之前不能提现');
            }
        }
        $wd_end=$rule->wd_end;
        if($wd_end){
            if($Hi>$wd_end){
                return $this->apiError($wd_end.'之后不能提现');
            }
        }
        //金额限制
        if(($money<$rule->wd_min)&&($rule->wd_min>0)){
            return $this->apiError('最小提现金额:'.$rule->wd_min);
        }
        if(($money>$rule->wd_max)&&($rule->wd_max>0)){
            return $this->apiError('最大提现金额:'.$rule->wd_max);
        }
        if(($money%$rule->wd_mul!=0)&&($rule->wd_mul>0)){
            return $this->apiError($rule->wd_mul.'的整数倍提现');
        }
        //账户限制
        if($pay_type=='bank'){
            if(!$user->bank_number){
                return $this->apiError('请完善银行收款信息');
            }
        }
        if($pay_type=='alipay'){
            if(!$user->alipay_number){
                return $this->apiError('请完善支付宝收款信息');
            }
        }
        if($pay_type=='wechat'){
            if(!$user->wechat_number){
                return $this->apiError('请完善微信收款信息');
            }
        }
        //余额判断
        $balance=WalletModel::where('currency_slug',$rule->currency_slug)->where('user_id',$user->id)->value('balance');
        if($money>$balance){
            return $this->apiError('余额不足');
        }
        //其他数据组装
        $cmoney=$rule->currency->price*$money;
        $fee=$cmoney*$rule->wd_fee*0.01;
        //需要支付的金额
        $pay_money=$cmoney-$fee;
        Db::beginTransaction();
        try {
            $return=[];
            //创建充值申请记录表
            $wd_record_table=new WdRecordModel();
            $wd_record_table->user_id=$user->id;
            $wd_record_table->currency_slug=$currency_slug;
            $wd_record_table->money=$money;
            $wd_record_table->fee=$fee;
            $wd_record_table->pay_money=$pay_money;
            $wd_record_table->pay_type=$pay_type;
            $wd_record_table->status=1;
            $wd_record_table->remark_user=$remark_user;
            $wd_record_table->save();
            //  提现扣除
            $commonapi=new CommonApi();
            $commonapi->settleDetailed($user->id,$user->id,$money,0,$rule->currency_slug,[
                'change_slug'=>'with_red',
                'table_id'=>$wd_record_table->id,
                'table_name'=>'summer_hjd_wd_record'

            ]);
            Db::commit();
            return $this->apiSuccess('操作成功',$return);
        }catch (Exception $e) {
            Db::rollBack();
            return $this->apiError($e->getMessage());
        }
    }

    /**
     * 提现记录
     */
    public function index(JWTAuth $auth)
    {
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $page=Input::get('page',1);
        $page_size=Input::get('page_size',20);
        $query=WdRecordModel::select();
        $query->with(['currency'=>function($q){
            $q->select('slug','title');
        }]);
        if(Input::has('currency_slug')){
            $currency_slug=Input::get('currency_slug');
            $query->where('currency_slug',$currency_slug);
        }
        if(Input::has('status')){
            $status=Input::get('status');
            $query->where('status',$status);
        }
        $query->where('user_id',$user->id);
        $query->orderBy('id','DESC');
        $records=$query->paginate($page_size,$page);
        $records->each(function ($record){
            $record->status_text=Config::get('summer.hjd::wd_status.'.$record->status);
            $record->pay_type_text=Config::get('summer.hjd::wd_pay_type.'.$record->pay_type);
        });
        $return['records']=$records;
        return $this->apiSuccess('操作成功',$return);
    }
}
